/**  
 * @Title SysUserServiceImpl.java
 * @date 2016年3月5日 下午3:31:43
 * @Copyright (c) 2016, unibroad.com Inc. All rights reserved.
 */
package com.strugglerz.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.strugglerz.biz.service.ISysUserService;
import com.strugglerz.dal.mapper.SysUserMapper;
import com.strugglerz.model.dalentity.SysUser;
import com.strugglerz.model.dalentity.SysUserCriteria;
import com.strugglerz.model.exception.BizHandleException;

/**
 * @Description 系统用户维护接口
 * @Class 
 * @author 
 * @version V1.0
 */
@Service
public class SysUserServiceImpl implements ISysUserService<SysUser> {

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public SysUser queryByName(String name) throws BizHandleException {
        SysUserCriteria example = new SysUserCriteria();
        SysUserCriteria.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        example.or(example.createCriteria().andMemoEqualTo(name));
        List<SysUser> users = sysUserMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(users)) {
            return users.get(0);
        }
        throw new BizHandleException("未查询到名称为 " + name + " 的用户!");
    }


    @Override
    @Transactional(rollbackFor = { Exception.class })
    public int insert(SysUser t) {
        t.setCreateTime(new Date());
        return sysUserMapper.insert(t);
    }


    @Override
    @Transactional(rollbackFor = { Exception.class })
    public int delete(SysUser t) {
        return sysUserMapper.deleteByPrimaryKey(t.getId());
    }


    @Override
    @Transactional(rollbackFor = { Exception.class })
    public int update(SysUser t) {
        return sysUserMapper.updateByPrimaryKey(t);
    }

}
